﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
using QLHD.Models;
using System.Globalization;

namespace QLHD.Account
{
    public partial class GroupUsers : SystemAdmin
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            base.Page_Load(sender, e);
            if (IsPostBack)
                return;
            BindCheckBoxList();
        }
        private void BindCheckBoxList()
        {

            if (Request.Params["GroupID"] != null)
            {
                QLHD2013Entities db = new QLHD2013Entities();

                List<UserUI> lst = new List<UserUI>();

                int groupID = int.Parse(Request.Params["GroupID"].ToString());
                Session["GroupID"] = groupID;
                List<User> lstuser;
                lstuser = db.Users.ToList();
                int i = 1;
                checkboxlist1.Attributes.Add("class", "gridchk");
                checkboxlist1.DataSource = lstuser;
                checkboxlist1.DataValueField = "ID";
                checkboxlist1.DataTextField = "FullName";
                checkboxlist1.DataBind();

                List<int> lstUser = db.GroupUsers.Where(q => q.GroupID == groupID).Select(q => q.UserID).ToList();
                //Select the list items based on the session value
                if (lstUser != null)
                {
                    // string[] bookItems = Session["BooksSelected"].ToString().Split(',');
                    foreach (int bookId in lstUser)
                    {
                        ListItem item = checkboxlist1.Items.FindByValue(bookId.ToString());
                        if (item != null)
                            item.Selected = true;
                    }
                }
            }
        }
        protected void cmdSavea_Click(object sender, EventArgs e)
        {
            string strNames = string.Empty;
            QLHD2013Entities db;
            
            QLHD.Models.GroupUser newobj;
            GroupPermissions objGP;
            foreach (ListItem chkItem in checkboxlist1.Items)
            {
                db = new QLHD2013Entities();
                newobj = new QLHD.Models.GroupUser();
                objGP = new GroupPermissions();

                int groupID = int.Parse(Request.QueryString["GroupID"].ToString());
                int userID = int.Parse(chkItem.Value);
                if (chkItem.Selected == true)
                {
                    var objGroupUser = db.GroupUsers.Where(q => q.GroupID == groupID && q.UserID == userID).FirstOrDefault();
                    if (objGroupUser == null)
                    {
                        newobj.GroupID = groupID;
                        newobj.UserID = userID;
                        db.AddToGroupUsers(newobj);
                    }

                    var objPU = db.PermissionsUsers.Where(p => p.GroupID == groupID && p.UserID == userID).FirstOrDefault();
                    if (objPU == null)
                    {
                        string query = "SELECT  distinct (itemid) as itemid, id, userid, groupid, objectID, PermissionValue from PermissionsUser where groupid=" + groupID;
                        List<PermissionsUser> objlist = db.ExecuteStoreQuery<PermissionsUser>(query, null).ToList();
                        foreach (PermissionsUser drPU in objlist)
                        {
                            QLHD2013Entities db1 = new QLHD2013Entities();
                            PermissionsUser objtemp = db1.PermissionsUsers.Where(q => q.GroupID == groupID && q.UserID == userID && q.ItemID == drPU.ItemID && q.PermissionValue == drPU.PermissionValue).FirstOrDefault();
                            if (objtemp == null)
                            {
                                objtemp = new PermissionsUser();
                                objtemp.ObjectID = drPU.ObjectID;
                                objtemp.UserID = userID;
                                objtemp.GroupID = groupID;
                                objtemp.ItemID = drPU.ItemID;
                                objtemp.PermissionValue = drPU.PermissionValue;
                                db1.AddToPermissionsUsers(objtemp);
                            }
                            db1.SaveChanges();
                        }
                    }
                }
                else
                {
                    var objPU = db.PermissionsUsers.Where(p => p.GroupID == groupID && p.UserID == userID).FirstOrDefault();
                    if (objPU != null)
                    {
                        string query = "delete from PermissionsUser where groupid=" + groupID + " and userid=" + userID;
                        db.ExecuteStoreQuery<DBNull>(query, null);
                    }
                    var objGroupUser = db.GroupUsers.Where(q => q.GroupID == groupID && q.UserID == userID).FirstOrDefault();
                    if (objGroupUser != null)
                    {
                        db.DeleteObject(objGroupUser);
                    }

                    // var objPU = db.ExecuteStoreQuery<PermissionsUser>(query, null);
                    //List<PermissionsUser> permisuser = db.ExecuteStoreQuery<PermissionsUser>(query, null).ToList();
                }
                db.SaveChanges();
            }
            Response.Redirect("GroupPermissionList.aspx");
        }
    }
}